package com.xiaozhen.alarm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.alarm.domain.AlarmStrategy;
import com.xiaozhen.alarm.service.AlarmStrategyService;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.domain.system.SysDept;
import com.xiaozhen.security.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/10/25 9:18
 * @description:
 */
@RestController
@Api(tags = "告警策略的接口")
@RequestMapping("/alarmStrategy")
public class AlarmStrategyController extends BaseController {

    @Autowired
    private AlarmStrategyService alarmStrategyService;

    @ApiOperation("获取所有策略列表")
    @GetMapping("/list")
    public AjaxResult list() {
        Page<AlarmStrategy> page = startPage();
        Page<AlarmStrategy> list = alarmStrategyService.selectAlarmStrategyList(page);
        return getDataTable(list);
    }


    @ApiOperation("新增策略")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody AlarmStrategy alarmStrategy) {
        if (!alarmStrategyService.checkStrategyNameUnique(alarmStrategy)) {
            return AjaxResult.error("新增策略'" + alarmStrategy.getStrategyName() + "'失败，策略名称已存在");
        }
        alarmStrategy.setCreateBy(getCurrentUserId());
        alarmStrategy.setCreateTime(LocalDateTime.now());
        return toAjax(alarmStrategyService.insertAlarmStrategy(alarmStrategy));
    }

    @ApiOperation("修改策略")
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody AlarmStrategy alarmStrategy) {
        if (!alarmStrategyService.checkStrategyNameUnique(alarmStrategy)) {
            return AjaxResult.error("新增策略'" + alarmStrategy.getStrategyName() + "'失败，策略名称已存在");
        }
        alarmStrategy.setUpdateBy(getCurrentUserId());
        alarmStrategy.setUpdateTime(LocalDateTime.now());
        return toAjax(alarmStrategyService.updateAlarmStrategy(alarmStrategy));
    }

    @ApiOperation("删除策略")
    @DeleteMapping("/{strategyId}")
    public AjaxResult remove(@PathVariable Long strategyId) {
        return toAjax(alarmStrategyService.deleteAlarmStrategyById(strategyId));
    }

}
